<template>
  <div id="app">
    <Header />
    <router-view class="router-view" v-slot="{Component}">
      <transition :name="transitionName">
        <component :is="Component" />
      </transition>
    </router-view>
    <Tabbar />
  </div>
</template>

<script setup>
import {ref, watch, defineAsyncComponent} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import {getDocumentTitle} from './utils/utils'

const route = useRoute()
const router = useRouter()

const transitionName = ref('slide-left')
const title = ref(route.meta.title)
const hidden = ref(false)

const Header = defineAsyncComponent(() => import('@/components/Header'))
const Tabbar = defineAsyncComponent(() => import('@/components/Tabbar'))

watch(
  () => route,
  newRoute => {
    title.value = route.meta.title
    if (newRoute.path === '/home') hidden.value = true
    else hidden.value = false
  },
  {
    immediate: true,
    deep: true
  }
)

router.beforeEach((to, from) => {
  if (to.meta.index > from.meta.index) {
    transitionName.value = 'slide-left'
  } else if (to.meta.index < from.meta.index) {
    transitionName.value = 'slide-right'
  } else {
    transitionName.value = ''
  }
})
</script>

<style lang="less">
html, body {
  height: 100%;
  overflow-x: hidden;
  overflow-y: scroll;
}
#app {
  height: 100%;
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}
.router-view{
  width: 100%;
  height: auto;
  position: absolute;
  top: 0;
  bottom: 0;
  margin: 44px auto 0;
  -webkit-overflow-scrolling: touch;
}
// 路由过渡动画
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active{
  height: 100%;
  will-change: transform;
  transition: all 500ms;
  position: absolute;
  backface-visibility: hidden;
}
.slide-right-enter{
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
.slide-right-leave-active{
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}
.slide-left-enter{
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}
.slide-left-leave-active{
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

.van-badge--fixed {
  z-index: 1000;
}
</style>
